//
//  HJInviteGuestController.m
//  Keping
//
//  Created by a on 2017/8/29.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "HJInviteGuestController.h"

#import "HJVisitFriendViewCell.h"

#import "HJSelectDateView.h"
#import "HJHeaderImgView.h"

@interface HJInviteGuestController ()<UITableViewDelegate,UITableViewDataSource,HJSelectDateViewDelegate>
/**
 tableView
 */
@property(nonatomic,weak)UITableView*tableView;
/**
 数据源
 */
@property(nonatomic,strong)NSMutableArray*dataAry;
@property(nonatomic,strong)NSMutableArray*InviteAry;
/**
 服务器获取数据
 */
@property(nonatomic,copy) NSArray *visitFriends;
@end

@implementation HJInviteGuestController

-(NSMutableArray *)dataAry
{
    if (!_dataAry) {
        _dataAry=[NSMutableArray arrayWithObjects:@[@"* Event Name"],@[@"* Date",@"* Time"], nil];
    }
    return _dataAry;
}
- (NSMutableArray *)InviteAry {
    if (!_InviteAry) {
        _InviteAry = [NSMutableArray arrayWithObjects:@[@""],@[@"",@""], nil];
    }
    return _InviteAry;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
//    [self getData];
}
-(void)setUI
{
    
    self.navigationItem.title=@"Invite Guests";
    UITableView*tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate=self;
    tableView.dataSource=self;
    tableView.backgroundColor=[UIColor colorWithHexString:@"#f2f1f6"];
    [self.view addSubview:tableView];
    self.tableView=tableView;
    [self.tableView registerNib:[UINib nibWithNibName:[HJDoorAccessCell className] bundle:nil] forCellReuseIdentifier:[HJDoorAccessCell className]];
    
    //设置头部
    HJHeaderImgView*headerView=[HJHeaderImgView headerImgView:KAppointmentTypeForInvite];
    tableView.tableHeaderView=headerView;
    
    //添加table尾部
    UIView*footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, K_FactorH(80))];
    tableView.tableFooterView=footerView;
    footerView.backgroundColor=[UIColor clearColor];
    CGFloat margin =K_FactorW(20);
    UIButton*addBtn=[[UIButton alloc]initWithFrame:CGRectMake(margin, K_FactorH(30), footerView.width-2*margin, footerView.height-K_FactorH(30))];
    [footerView addSubview:addBtn];
    addBtn.layer.cornerRadius=addBtn.height/2;
    addBtn.layer.masksToBounds=YES;
    [addBtn setBackgroundColor:[UIColor colorWithHexString:@"#6853fa"] forState:UIControlStateNormal];
    [addBtn setTitle:@"Next Step" forState:UIControlStateNormal];
    addBtn.titleLabel.font=[UIFont boldSystemFontOfSize:20];
    [addBtn addTarget:self action:@selector(clickNextStep) forControlEvents:UIControlEventTouchUpInside];
}

-(void)clickNextStep
{
    NSString *date = self.InviteAry[1][0];
    if ([date isEqualToString:@""]) {
        ALERT(@"请选择日期!");
        return;
    }
    NSString *name = self.InviteAry[0][0];
    if ([name isEqualToString:@""]) {
        ALERT(@"请输入活动名称!");
        return;
    }
    NSString *time = self.InviteAry[1][1];
    if ([time isEqualToString:@""]) {
        ALERT(@"请选择日期!");
        return;
    }
    HJInviteEntitysAddRequest *request = [[HJInviteEntitysAddRequest alloc] init];
    request.params = @{
                       @"date":date,
                       @"eventName":name,
                       @"time":[NSString stringWithFormat:@"%@:00",time]
                       };
    WEAKSELF
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        NSDictionary *dict = request.responseObject;
        NSInteger status = [[dict objectForKey:@"statusCode"] integerValue];
        if (status == 200) {
            HJInviteGuestSelectVisitationVC *vc = [[HJInviteGuestSelectVisitationVC alloc] init];
            vc.inviteEntityID = dict[@"inviteEntityView"][@"id"];
            vc.date = date;
            vc.time = time;
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            [weakSelf showErrorText:[dict objectForKey:@"msg"] errCode:status];
        }
        
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf showError:request.error];
    }];
}
#pragma mark --UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataAry.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataAry[section] count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HJVisitFriendViewCell*cell=[HJVisitFriendViewCell cellWithTableView:tableView];
    NSInteger section = indexPath.section;
    NSInteger row     = indexPath.row;
    NSString *title = self.dataAry[indexPath.section][row];
    cell.title = title;
    NSString *content = self.InviteAry[section][row];
    cell.content = content;
    WEAKSELF
    cell.visitFriend = ^(id cell, NSString *contet) {
        NSMutableArray *arr = [weakSelf.InviteAry[0] mutableCopy];
        [arr replaceObjectAtIndex:0 withObject:contet];
        [weakSelf.InviteAry replaceObjectAtIndex:0 withObject:arr];
    };

    if (section == 0) {
        cell.isEdit = YES;
    } else {
        cell.isEdit = NO;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==0&&indexPath.row==0) {
        return;
    }else if (indexPath.section==1)
    {
        //弹出时间选择器
        if (indexPath.row==0) {
            //弹出年月日
            [self creatDatePicker:UIDatePickerModeDate];
        }else if (indexPath.row==1)
        {
            //弹出时分秒
            [self creatDatePicker:UIDatePickerModeTime];
        }
    }else
    {
        //选择好友
        id friend = self.dataAry[indexPath.section][indexPath.row];
        if ([friend isKindOfClass:[NSString class]]) {
            ALERT(friend);
        } else {
            RelationFriendViews *relation = friend;
            NSArray *ary = @[relation];
            [self.dataAry replaceObjectAtIndex:0 withObject:ary];
            [self.tableView reloadData];
        }
    }
}
-(void)creatDatePicker:(UIDatePickerMode)dateModel
{
    HJSelectDateView*dateView=[[HJSelectDateView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, APP_Frame_Height)];
    dateView.delegate=self;
    dateView.DateModel=dateModel;
    [self.navigationController.view addSubview:dateView];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return K_FactorH(60);
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return K_FactorH(10);
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}

#pragma mark -- HJSelectDateViewDelegate
-(void)selectDateView:(HJSelectDateView*)selectDateView clickSelectDate:(NSString*)dateStr indexPath:(NSIndexPath*)indexPath {
    NSMutableArray *arr = [self.InviteAry[1] mutableCopy];
    if (selectDateView.DateModel == UIDatePickerModeDate) {
        [arr replaceObjectAtIndex:0 withObject:dateStr];
    } else {
        [arr replaceObjectAtIndex:1 withObject:dateStr];
    }
    [self.InviteAry replaceObjectAtIndex:1 withObject:arr];
    [self.tableView reloadData];
}


@end
